﻿using System;
using System.Runtime.Serialization;
using Framework.Exceptions;
using Interfaces.BusinessLogic;
using Interfaces.Infrastructure;
using Interfaces.Models;


namespace BusinessLogic.Services.Security
{
    public class InternalSecurityService : IInternalSecurityService
    {
        private readonly IUserOperationService _userOperationService;
        private readonly ITiketSerializer _tiketSerializer;
        private readonly ISessionOperationService _sessionOperationService;


        public InternalSecurityService(IUserOperationService userOperationService, 
            ITiketSerializer tiketSerializer, 
            ISessionOperationService sessionOperationService)
        {
            _userOperationService = userOperationService;
            _tiketSerializer = tiketSerializer;
            _sessionOperationService = sessionOperationService;
        }

        public string GetTiket(string login, string password, string email)
        {
            var user = _userOperationService.GetUser(login, email, password);

            var session = _sessionOperationService.GetActiveSession(user.Id)
                    ?? _sessionOperationService.Create(user);
            try
            {
                var ticket  = _tiketSerializer.Serialize(session);

                if(ticket == null)
                    throw new SerializationException("Ticket can't be serialize");

                return ticket;
            }
            catch (Exception exception)
            {
                if (ExceptionHandlingManager.HandleException(exception, PolicyNames.DALPolicy))
                    throw;
            }

            return null;
        }

        public ISession DeserializeTiket(string ticket)
        {
            ISession userSession = null;
            
            try
            {
                //todo maybe i should change result for this operation on session id
                if (string.IsNullOrEmpty(ticket))
                    throw new ArgumentException("tiket");
                
                var session = _tiketSerializer.Deserialize(ticket);
                userSession = _sessionOperationService.GetSession(session.Id);
                
            }
            catch (Exception exception)
            {
                if (ExceptionHandlingManager.HandleException(exception, PolicyNames.DALPolicy))
                    throw;
            }

            return userSession;
          
        }

        public void DisableSession(ISession session)
        {
            try
            {
                if (session != null)
                    _sessionOperationService.Remove(session.Id);
            }
            catch (Exception exception)
            {
                if (ExceptionHandlingManager.HandleException(exception, PolicyNames.DALPolicy))
                    throw;
            }
           
        }

        public bool VerifySession(ISession session)
        {
            var result = false;
            try
            {
                result = _sessionOperationService.Validate(session).IsValid;
            }
            catch (Exception exception)
            {
                if (ExceptionHandlingManager.HandleException(exception, PolicyNames.DALPolicy))
                    throw;
            }

            return result;

        }
        
    }
}
